﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace WindowsForms
{
    
    public partial class FrmClubMemberManage : Form
    {
        public FrmClubMemberManage()
        {
            InitializeComponent();
            #region   初始化控件缩放

            x = Width;
            y = Height;
            setTag(this);

            #endregion
        }

        private void FrmClubMemberManage2_Load(object sender, EventArgs e)
        {
            MemberManage memberbll = new MemberManage();
            dgvMember1.AutoGenerateColumns = false;
            string sql = "select * from member,club,department where member.clubid=club.clubid and member.departmentid=department.departmentid";
            dgvMember1.DataSource = memberbll.GetMembersByCondition(sql);



            ClubMember cm1 = new ClubMember("18623405", "张小明", "男", Convert.ToDateTime("2001-4-8"), "2018", 2, 2, 2, "36543000", "1387676", "tx1.jpg", "体育", "一级运动员");
            ClubMember cm2 = new ClubMember("186405", "明", "男", Convert.ToDateTime("2001-6-8"), "2018", 2, 3, 1, "36543000", "13876", "tx2.jpg", "体育", "热爱音乐");
            ClubMember cm3 = new ClubMember("1863405", "小明", "男", Convert.ToDateTime("2051-4-8"), "2018", 2, 1, 2, "345643000", "137676", "tx3.jpg", "文艺", "一级运动员");
            ClubMember cm4 = new ClubMember("186345", "张明", "男", Convert.ToDateTime("2002-4-8"), "2018", 2, 3, 1, "34565000", "13876", "tx4.jpg", "体育", "学生干部");
            ClubMember cm5 = new ClubMember("1862405", "张小", "男", Convert.ToDateTime("2056-4-8"), "2018", 2, 3, 1, "34543000", "137676", "zxm.jpg", "体育", "一级运动员");
            lstMemberList.Items.Add(cm1);
            lstMemberList.Items.Add(cm2);
            lstMemberList.Items.Add(cm3);
            lstMemberList.Items.Add(cm4);
            lstMemberList.Items.Add(cm5);
            lstMemberList.DisplayMember = "Name";



            Department[] deptArray = new Department[4];
            deptArray[0] = new Department(1, "信息工程系");
            deptArray[1] = new Department(2, "电子工程系");
            deptArray[2] = new Department(3, "管理工程系");
            deptArray[3] = new Department(4, "机电工程系");
            cmbDepartment.DataSource = deptArray;
            cmbDepartment.DisplayMember = "Departmentname";
            cmbDepartment.ValueMember = "Departmentid";

            Profession[] professionArray = new Profession[17];
            professionArray[0] = new Profession(1, 1, "软件技术");
            professionArray[1] = new Profession(2, 1, "计算机应用");
            professionArray[2] = new Profession(3, 2, "应用电子技术");
            professionArray[3] = new Profession(4, 3, "电子商务");
            professionArray[4] = new Profession(5, 3, "会计");
            professionArray[5] = new Profession(6, 3, "旅游");
            professionArray[6] = new Profession(7, 1, "动漫");
            professionArray[7] = new Profession(8, 1, "网络技术");
            professionArray[8] = new Profession(9, 4, "电气自动化");
            professionArray[9] = new Profession(10, 4, "机电一体化");
            professionArray[10] = new Profession(11, 4, "模具设计与制造");
            professionArray[11] = new Profession(12, 4, "数控技术");
            professionArray[12] = new Profession(13, 2, "电子测量与仪器");
            professionArray[13] = new Profession(14, 2, "电子信息技术");
            professionArray[14] = new Profession(15, 3, "物流管理");
            professionArray[15] = new Profession(16, 3, "商务英语");
            professionArray[16] = new Profession(17, 3, "商务日语");
            cmbProfession.DataSource = professionArray;
            cmbProfession.DisplayMember = "ProfessionName";
            cmbProfession.ValueMember = "Professionid";


            ClubMember cm = (ClubMember)dgvMember1.CurrentRow.DataBoundItem;
            LoadDetail(cm);

        }
        void LoadDetail(ClubMember cm)
        {
            txtMemberID.Text = cm.Studentid;
            txtName.Text = cm.Name;
            if (cm.Sex == "男")
            {
                rdoBoy.Checked = true;
            }
            else
            {
                rdoGirl.Checked = true;
            }
            dtpBrithday.Value = cm.Birthday;
            //if (cm.Pic != null)
            //{

            //}
            cmbGrade.Text = cm.Grade;
            cmbDepartment.SelectedValue = cm.Departmentid;
            cmbProfession.SelectedValue = cm.Professionid;
            txtQQ.Text = cm.Qq;
            txtPhone.Text = cm.Phone;
            txtMemo.Text = cm.Memo;
            chkSports.Checked = false;
            chkLiterature.Checked = false;
            chkTravel.Checked = false;
            chkDrawing.Checked = false;
            string[] hobbies = cm.Hobby.Split(';');
            foreach (string h in hobbies)
            {
                switch (h)
                {
                    case "体育": chkSports.Checked = true; break;
                    case "文艺": chkLiterature.Checked = true; break;
                    case "书画": chkDrawing.Checked = true; break;
                    case "旅游": chkTravel.Checked = true; break;
                }
            }
            txtMemo.Text = cm.Memo;

        }

        private void dgvMember1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            ClubMember cm = (ClubMember)dgvMember1.CurrentRow.DataBoundItem;
            LoadDetail(cm);
        }
        
        

        private void lstMemberList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstMemberList.SelectedIndex != -1)
            {
                ClubMember cm = (ClubMember)lstMemberList.SelectedItem;
                txtMemberID.Text = cm.Studentid;
                txtName.Text = cm.Name;
                if (cm.Sex == "男")
                {
                    rdoBoy.Checked = true;
                }
                else
                {
                    rdoGirl.Checked = true;
                }
                dtpBrithday.Value = cm.Birthday;
                if (cm.Pic != "")
                {
                    picPicture.Image = Image.FromFile(cm.Pic);
                }
                else
                {
                    picPicture.Image = Image.FromFile("nopic.jpg");
                }
                cmbGrade.Text = cm.Grade;
                cmbDepartment.SelectedValue = cm.Departmentid;
                cmbProfession.SelectedValue = cm.Professionid;
                txtQQ.Text = cm.Qq;
                txtPhone.Text = cm.Phone;
                txtMemo.Text = cm.Memo;
                chkSports.Checked = false;
                chkLiterature.Checked = false;
                chkTravel.Checked = false;
                chkDrawing.Checked = false;
                string[] hobbies = cm.Hobby.Split(';');
                foreach (string h in hobbies)
                {
                    switch (h)
                    {
                        case "体育": chkSports.Checked = true; break;
                        case "文艺": chkLiterature.Checked = true; break;
                        case "书画": chkDrawing.Checked = true; break;
                        case "旅游": chkTravel.Checked = true; break;
                    }
                }
                txtMemo.Text = cm.Memo;
            }
        }

        

        private void txtMemberID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar) && e.KeyChar != 22 && e.KeyChar != 3)
            {
                MessageBox.Show("请输入数字", "系统提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Handled = true;
            }
        }

        private void txtMemberID_KeyUp(object sender, KeyEventArgs e)
        {
            if (txtMemberID.TextLength > 10)
            {
                MessageBox.Show("社员编号长度为10！", "系统提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtMemberID.Text = txtMemberID.Text.Substring(0, 10);
            }
        }

        private void txtMemberID_Leave(object sender, EventArgs e)
        {
            if (txtMemberID.TextLength > 10)
            {
                MessageBox.Show("社员编号长度为10！", "系统提示",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtMemberID.Focus();
            }
        }

        private void txtQQ_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar) && e.KeyChar != 22 && e.KeyChar != 3)
            {
                MessageBox.Show("请输入数字", "系统提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Handled = true;
            }
        }

        private void txtPhone_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar) && e.KeyChar != 22 && e.KeyChar != 3)
            {
                MessageBox.Show("请输入数字", "系统提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Handled = true;
            }
        }

        private void txtPhone_KeyUp(object sender, KeyEventArgs e)
        {
            if (txtPhone.TextLength > 11)
            {
                MessageBox.Show("手机号长度为11位！", "系统提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPhone.Text = txtPhone.Text.Substring(0, 11);
            }
        }

        private void txtPhone_Leave(object sender, EventArgs e)
        {
            if (txtPhone.TextLength > 11)
            {
                MessageBox.Show("手机号长度为11位！", "系统提示",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPhone.Focus();
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            string studentid = txtMemberID.Text;
            string name = txtName.Text;
            string sex = "";
            if (rdoBoy.Checked)
            {
                sex = "男";
            }
            else
            {
                sex = "女";
            }
            DateTime birthday = dtpBrithday.Value;
            string grade = cmbGrade.Text;
            int departmentid = Convert.ToInt32(cmbDepartment.SelectedValue);
            int professionid = Convert.ToInt32(cmbProfession.SelectedValue);
            int clubid = Convert.ToInt32(cmbDepartment.SelectedValue);
            string pic = "";
            string qq = txtQQ.Text;
            string phone = txtPhone.Text;
            string memo = txtMemo.Text;
            string hobbies = "";
            if (chkSports.Checked)
            {
                hobbies += chkSports.Text + ";";
            }
            if (chkLiterature.Checked)
            {
                hobbies += chkLiterature.Text + ";";
            }
            if (chkTravel.Checked)
            {
                hobbies += chkTravel.Text + ";";
            }
            if (chkDrawing.Checked)
            {
                hobbies += chkDrawing.Text + ";";
            }
            ClubMember cm = new ClubMember(studentid, name, sex, birthday, grade, departmentid, professionid, clubid, qq, phone, pic, hobbies, memo);
            lblMessage.Text = cm.GetInfo();
            lstMemberList.Items.Add(cm);



            MemberManage memberbll = new MemberManage();
            if (memberbll.AddMember(cm))
                MessageBox.Show("社员保存成功!");
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            string studentid = txtMemberID.Text;
            string name = txtName.Text;
            string sex = "";
            if (rdoBoy.Checked)
            {
                sex = "男";
            }
            else
            {
                sex = "女";
            }
            DateTime birthday = dtpBrithday.Value;
            string grade = cmbGrade.Text;
            int departmentid = Convert.ToInt32(cmbDepartment.SelectedValue);
            int professionid = Convert.ToInt32(cmbProfession.SelectedValue);
            int clubid = Convert.ToInt32(cmbDepartment.SelectedValue);
            string pic = "";
            string qq = txtQQ.Text;
            string phone = txtPhone.Text;
            string memo = txtMemo.Text;
            string hobbies = "";
            if (chkSports.Checked)
            {
                hobbies += chkSports.Text + ";";
            }
            if (chkLiterature.Checked)
            {
                hobbies += chkLiterature.Text + ";";
            }
            if (chkTravel.Checked)
            {
                hobbies += chkTravel.Text + ";";
            }
            if (chkDrawing.Checked)
            {
                hobbies += chkDrawing.Text + ";";
            }
            ClubMember cm = new ClubMember(studentid, name, sex, birthday, grade, departmentid, professionid, clubid, qq, phone, pic, hobbies, memo);
            lblMessage.Text = cm.GetInfo();
            lstMemberList.Items.Add(cm);



            MemberManage memberbll = new MemberManage();
            if (memberbll.AddMember(cm))
                MessageBox.Show("社员添加成功!");
        }
       

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string studentid = txtMemberID.Text;
            string name = txtName.Text;
            string sex = "";
            if (rdoBoy.Checked)
            {
                sex = "男";
            }
            else
            {
                sex = "女";
            }
            DateTime birthday = dtpBrithday.Value;
            string grade = cmbGrade.Text;
            int departmentid = Convert.ToInt32(cmbDepartment.SelectedValue);
            int professionid = Convert.ToInt32(cmbProfession.SelectedValue);
            int clubid = Convert.ToInt32(cmbDepartment.SelectedValue);
            string pic = "";
            string qq = txtQQ.Text;
            string phone = txtPhone.Text;
            string memo = txtMemo.Text;
            string hobbies = "";
            if (chkSports.Checked)
            {
                hobbies += chkSports.Text + ";";
            }
            if (chkLiterature.Checked)
            {
                hobbies += chkLiterature.Text + ";";
            }
            if (chkTravel.Checked)
            {
                hobbies += chkTravel.Text + ";";
            }
            if (chkDrawing.Checked)
            {
                hobbies += chkDrawing.Text + ";";
            }
            ClubMember cm = new ClubMember(studentid, name, sex, birthday, grade, departmentid, professionid, clubid, qq, phone, pic, hobbies, memo);
            MemberManage memberbll = new MemberManage();
            if (memberbll.UpdateMember(cm))
                MessageBox.Show("社员修改成功!");
        }

        private void txtKey_KeyPress(object sender, KeyPressEventArgs e)
        {
            //列表查询
            /*bool find = false;
            if (e.KeyChar == 13)
            {
                for (int i = 0; i < lstMemberList.Items.Count; i++)
                {
                    ClubMember cm = (ClubMember)lstMemberList.Items[i];
                    if (cm.Name == txtKey.Text)
                    {
                        find = true;
                        MessageBox.Show("找到社员" + txtKey.Text, "系统提示",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                        lstMemberList.SelectedIndex = i;
                        break;
                    }
                }
                if (find == false)
                {
                    MessageBox.Show("未找到社员" + txtKey.Text, "系统提示",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }*/
            //数据库查询
               bool find = false;  
                if (e.KeyChar == 13)  
                {  
                    foreach (DataGridViewRow row in dgvMember1.Rows)  
                    {  
                        ClubMember cm = (ClubMember)row.DataBoundItem;  
                        if (cm.Name == txtKey.Text)  
                        {  
                            find = true;  
                            MessageBox.Show("找到社员 " + txtKey.Text, "系统提示",  
                                MessageBoxButtons.OK, MessageBoxIcon.Information);  
                            dgvMember1.ClearSelection(); // 清除当前选择  
                            row.Selected = true; // 将找到的行设置为选中状态  
                            LoadDetail(cm);  
                            break; // 找到后退出循环  
                        }  
                    }  
                    if (!find)
                    {  
                        MessageBox.Show("未找到社员 " + txtKey.Text, "系统提示",  
                            MessageBoxButtons.OK, MessageBoxIcon.Information);  
                    }  
                }
            }

        private void FrmClubMemberManage_Resize(object sender, EventArgs e)
        {
            //重置窗口布局
            ReWinformLayout();
        }

        #region 控件大小随窗体大小等比例缩放

        private readonly float x; //定义当前窗体的宽度
        private readonly float y; //定义当前窗体的高度

        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0) setTag(con);
            }
        }

        private void setControls(float newx, float newy, Control cons)
        {
            //遍历窗体中的控件，重新设置控件的值
            foreach (Control con in cons.Controls)
                //获取控件的Tag属性值，并分割后存储字符串数组
                if (con.Tag != null)
                {
                    var mytag = con.Tag.ToString().Split(';');
                    //根据窗体缩放的比例确定控件的值
                    con.Width = Convert.ToInt32(Convert.ToSingle(mytag[0]) * newx); //宽度
                    con.Height = Convert.ToInt32(Convert.ToSingle(mytag[1]) * newy); //高度
                    con.Left = Convert.ToInt32(Convert.ToSingle(mytag[2]) * newx); //左边距
                    con.Top = Convert.ToInt32(Convert.ToSingle(mytag[3]) * newy); //顶边距
                    var currentSize = Convert.ToSingle(mytag[4]) * newy; //字体大小                   
                    if (currentSize > 0) con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    con.Focus();
                    if (con.Controls.Count > 0) setControls(newx, newy, con);
                }
        }


        /// <summary>
        /// 重置窗体布局
        /// </summary>
        private void ReWinformLayout()
        {
            var newx = Width / x;
            var newy = Height / y;
            setControls(newx, newy, this);

        }



        #endregion

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lstMemberList.SelectedIndex != -1)
            {
                lstMemberList.Items.Remove(lstMemberList.SelectedItem);
                lstMemberList.SelectedIndex = 0;
            }
            string studentid = txtMemberID.Text;
            string name = txtName.Text;
            string sex = "";
            if (rdoBoy.Checked)
            {
                sex = "男";
            }
            else
            {
                sex = "女";
            }
            DateTime birthday = dtpBrithday.Value;
            string grade = cmbGrade.Text;
            int departmentid = Convert.ToInt32(cmbDepartment.SelectedValue);
            int professionid = Convert.ToInt32(cmbProfession.SelectedValue);
            int clubid = 0;
            string pic = "";
            string qq = txtQQ.Text;
            string phone = txtPhone.Text;
            string memo = txtMemo.Text;
            string hobbies = "";
            if (chkSports.Checked)
            {
                hobbies += chkSports.Text + ";";
            }
            if (chkLiterature.Checked)
            {
                hobbies += chkLiterature.Text + ";";
            }
            if (chkTravel.Checked)
            {
                hobbies += chkTravel.Text + ";";
            }
            if (chkDrawing.Checked)
            {
                hobbies += chkDrawing.Text + ";";
            }
            ClubMember cm = new ClubMember(studentid, name, sex, birthday, grade, departmentid, professionid, clubid, qq, phone, pic, hobbies, memo);
            MemberManage memberbll = new MemberManage();
            if (memberbll.DeleteMember(cm))
                MessageBox.Show("社员删除成功!");
        }
    }
}
